package com.by.common.core.exception;

import com.by.common.core.bean.ResultBean;
import com.by.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

@RestControllerAdvice
public class GlobalExceptionHandler extends BaseController {



    @ExceptionHandler(Exception.class)
    public ResultBean handlerException(Exception e) {
        e.printStackTrace();
        return fail("接口请求异常");
    }


    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidateException(ValidateException e){
        return fail("验证错误", e.getError());
    }

    @ExceptionHandler(FileSizeLimitException.class)
    public ResultBean handlerFileSizeLimitException(FileSizeLimitException e){
        return fail(e.getMessage());
    }

    @ExceptionHandler(FileNotExistException.class)
    public ResultBean handlerFileNotExistException(FileNotExistException e) {
        return fail(e.getMessage());
    }

    @ExceptionHandler({MultipartException.class})
    public ResultBean handlerUploadException(MultipartException e) {
        e.printStackTrace();
        return fail("上传文件超过指定大小！");
    }

    /**
     * 统一处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler({BusinessException.class})
    public ResultBean handlerBusinessException(BusinessException e) {
        e.printStackTrace();
        return fail(e.getMessage());
    }

}
